# code moved from main.py

import Tkinter
import icons
import mutil
import shellclasses
from importpyrind import pyrind
Callable = pyrind.wrappers.Callable 


class MainAux():
	def addContextButtons(self):
		self.contextButtons = {}
		btnsData = (
			(icons.iconDots,'props',self.onclickContextProps),
			(icons.iconResize,'move',None),
			(icons.iconId,'setid',self.onclickContextSetid),
			(icons.iconCaret,'settext',self.onclickContextSettext),
			(icons.iconStar,'action',self.onclickContextAction),
			)
		for icon, name, event in btnsData:
			b = Tkinter.Button(self.topframe._basecontrol, command=event)
			icons.addIconToWidget(b, icon)
			b.config(borderwidth=0)
			self.contextButtons[name] = b
			b.place(x=-300, y=-300)
			b.config(state=Tkinter.NORMAL)
		self.contextButtons['move'].bind('<Button-1>',self.onclickContextMoveMouseDown)
		self.contextButtons['move'].bind('<B1-Motion>',self.onclickContextMoveMouseMove)
		self.contextButtons['move'].bind('<ButtonRelease-1>',self.onclickContextMoveMouseUp)
	
	def _whichButtons(self, pyobject):
		if isinstance(pyobject, pyrind.RButton): return ('settext','action','move')
		elif isinstance(pyobject, pyrind.RInput): return ('setid','move')
		elif isinstance(pyobject, pyrind.RCheckbox): return ('settext','setid','move')
		elif isinstance(pyobject, pyrind.RField): return ('settext','setid','move')
		elif isinstance(pyobject, pyrind.RLabel): return ('settext','move')
		elif isinstance(pyobject, pyrind.RListbox): return ('setid','move')
		
	
	def showContextButtons(self, pyobject):
		self.hideContextButtons()
		pyobject._basecontrol.update_idletasks() #to get dimenesions
		lb = self._whichButtons(pyobject)
		px = 0
		for b in lb:
			b=self.contextButtons[b]
			b.place(x=px+pyobject._basecontrol.winfo_x(), y=pyobject._basecontrol.winfo_y()+pyobject._basecontrol.winfo_height())
			b.config(state=Tkinter.NORMAL)
			px += 25
			
	def hideContextButtons(self):
		for b in self.contextButtons:
			self.contextButtons[b].place(x=-300, y=-300)
			self.contextButtons[b].config(state=Tkinter.DISABLED) #so that doesn't have focus accidentally



	def onclickContextProps(self):
		pass
	def onclickContextMoveMouseDown(self, e):
		self.contextMoveStoredX = e.x
		self.contextMoveStoredY = e.y
		self.contextMoveStoredDX = self.contextButtons['move'].winfo_x() - self.selectedPyrObject._basecontrol.winfo_x() 
		self.contextMoveStoredDY = self.contextButtons['move'].winfo_y() - self.selectedPyrObject._basecontrol.winfo_y() 
		#~ print self.contextMoveStoredDX, self.contextMoveStoredDY
	def onclickContextMoveMouseMove(self, e):
		b = self.contextButtons['move']
		x =  e.x_root - self.window._basecontrol.winfo_rootx() - self.contextMoveStoredX
		y =  e.y_root - self.window._basecontrol.winfo_rooty() - self.contextMoveStoredY
		#~ print x,y
		#~ b.place(x=x, y=y)
		#~ self.selectedPyrObject._basecontrol.place(x=x-self.contextMoveStoredDX, y=y-self.contextMoveStoredDY)
		
		basex = x-self.contextMoveStoredDX; basey = y-self.contextMoveStoredDY; 
		self.selectedPyrObject._basecontrol.place(x=basex, y=basey)
		lb = self._whichButtons(self.selectedPyrObject)
		bx = basex
		by = basey+self.selectedPyrObject._basecontrol.winfo_height()
		for b in lb:
			self.contextButtons[b].place(x=bx, y=by)
			bx += 25
	
	def _getNewIdName(self):
		name = pyrind.dialog.ask('Name this widget (a-z and 0-9 only):', self.selectedModelObject.widgetName, title="")
		if name==None or not name or not mutil.makeIdentifier(name):
			return
		name = mutil.makeIdentifier(name)
		#see if that already exists
		for widget in self.objectModel.widgets:
			if widget.widgetName==name:
				pyrind.dialog.alert('Already a widget with that name.')
				return None
		return name
	
	def onclickContextMoveMouseUp(self, e):
		self.selectedModelObject.coordLeft = self.selectedPyrObject._basecontrol.winfo_x()
		self.selectedModelObject.coordTop = self.selectedPyrObject._basecontrol.winfo_y()
		
	def onclickContextSettext(self):
		if self.selectedPyrObject==None: print '?'; return
		text = pyrind.dialog.ask('Text:', self.selectedPyrObject.text, title="")
		if text!=None:
			self.selectedPyrObject.text = text
			self.selectedModelObject.pyrindProperties['text'] = text
	def onclickContextSetid(self):
		if self.selectedPyrObject==None: print '?'; return
		name = self._getNewIdName()
		if not name: return
		self.selectedModelObject.widgetName = name
	def onclickContextAction(self):
		if self.selectedPyrObject==None: print '?'; return
		assert isinstance(self.selectedPyrObject, pyrind.RButton)
		assert isinstance(self.selectedModelObject, shellclasses.MeringueProjectButton)
		if self.selectedModelObject.hasOnclickBinding:
			pyrind.dialog.alert('Onclick binding already created for button named "%s".'%self.selectedModelObject.widgetName)
		else:
			name = self._getNewIdName()
			if not name: return
			self.selectedModelObject.widgetName = name
			self.selectedModelObject.hasOnclickBinding = True
			pyrind.dialog.alert('Onclick binding was created for button named "%s".'%self.selectedModelObject.widgetName)
		
	def addMenuBar(self, w):
		w.add_menu('&File')
		w.add_items('&New Project|Ctrl+N',self.newProject,
				'&Open Project|Ctrl+O',self.openProject,
				'&Save Project|Ctrl+S',self.saveProject,
				'___', None,
				'&Run Project|Ctrl+R',self.runProject,
				#~ '&Build Project|Ctrl+B',self.buildProject,
				'___', None,
				'E&xit',self.notImpl,
				)
		w.add_menu('&Edit')
		w.add_items('&Undo|Ctrl+Z',self.notImpl,
				'&Redo|Ctrl+Y',self.notImpl,
				'___', None,
				'Cu&t widget|Ctrl+X',self.notImpl,
				'&Copy widget|Ctrl+C',self.notImpl,
				'&Paste widget|Ctrl+V',self.notImpl,
				'&Delete widget',self.removeSelectedWidget,
				'___', None,
				'&Alignment|Ctrl+L',self.notImpl,
				'&Duplicate|Ctrl+D',self.notImpl,
				'___', None,
				'Preferences...',self.notImpl,
				)
		w.add_menu('&Items')
		for name,o in self.supportedObjs:
			w.add_item('Add '+name, Callable(self.createNewWidgetFromPalette,o))
			
		w.add_menu('&Help')
		w.add_items('&About',self.helpAbout,
				)

